package shared;

import java.nio.FloatBuffer;

public class Button extends GUIBase{

	NinePatch m_patch;
	String m_string;
	NuFont m_font;
	//encapsulation of patch, means gui elements don't inherit boxes
	Vec2f m_pos;
	Vec2f m_size;
	int m_ID;
	
	public Button(Vec2f pos, Vec2f size, int[] textureIDs, int offset, MyListener listener,String string, int ID)
	{
		m_pos=pos;
		m_size=size;
		m_listener=listener;
		m_string=string;
		//build ninepatch
		m_patch=new NinePatch(pos,size.x,size.y,textureIDs[offset]);
		m_ID=ID;
		m_font=new NuFont(pos, textureIDs[offset+1], 64,0.6F);
		m_font.setString(m_string);
		
	}
	
	@Override
	public void Update()
	{
	
	}
	
	@Override
	public void Draw(FloatBuffer buffer, int matrixloc)
	{
		m_patch.Draw(buffer, matrixloc);
		//draw text
		//m_font.drawString(m_pos.x, m_pos.y, m_string);
		m_font.Draw(buffer, matrixloc);
	}

	@Override
	public boolean ClickEvent(Vec2f pos) {
		// TODO Auto-generated method stub
		//check inside
		if (pos.x>m_pos.x && pos.x<m_pos.x+m_size.x)
		{
			if (pos.y>m_pos.y && pos.y<m_pos.y+m_size.y)
			{
				m_listener.ButtonCallback(m_ID, pos);
			}
		}		
		
		return false;
	}

	@Override
	public void Discard() {
		// TODO Auto-generated method stub
		m_patch.Discard();
		m_font.Discard();
		
	}	
	
	public void AdjustPos(Vec2f p)
	{
	

		m_pos.x+=p.x;
		m_pos.y+=p.y;
		Vec2f pp=new Vec2f(m_pos.x+0.25F, m_pos.y+(m_size.y/2)-0.25F);	
		m_font.AdjustPos(pp);	
		m_patch.AdjustPos(m_pos);
	}
}
